package com.huahang.fashionpress.service.impl;

import com.huahang.fashionpress.enums.ExceptionEnum;
import com.huahang.fashionpress.exception.ResultException;
import com.huahang.fashionpress.form.FormNick;
import com.huahang.fashionpress.mapper.AdminMapper;
import com.huahang.fashionpress.mapper.NickMapper;
import com.huahang.fashionpress.mapper.NickroleMapper;
import com.huahang.fashionpress.mapper.RoleMapper;
import com.huahang.fashionpress.model.Admin;
import com.huahang.fashionpress.model.Nick;
import com.huahang.fashionpress.model.Role;
import com.huahang.fashionpress.service.AdminService;
import com.huahang.fashionpress.service.NickcService;
import com.huahang.fashionpress.vo.NickVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
@Slf4j
public class NickServiceImpl implements NickcService {
    @Resource
    NickMapper nickMapper;
    @Resource
    NickroleMapper nickroleMapper;
    @Resource
    RoleMapper RoleMapper;
    @Resource
    AdminMapper adminMapper;
    //查询所有角色信息
    @Override
    public List<NickVO> selectAllFNick() {
        List<NickVO> formNickList = new ArrayList<>();
        List<Nick> nickList = nickMapper.selectAllNicks();
        for (Nick nick:nickList) {
            NickVO nickVO = new NickVO();
            nickVO.setNickId(nick.getNickId());
            nickVO.setNickName(nick.getNickName());
            List<Role> roleList = nickroleMapper.selectAllroleIdBynickId(nick.getNickId());
//            List<String> roleNameList = new ArrayList<>();
//            for(Integer roleId:roleIdlist){
//                String roleName = RoleMapper.selectroleNameByroleId(roleId);
//                roleNameList.add(roleName);
//            }
            nickVO.setRoleList(roleList);
            formNickList.add(nickVO);
        }
        return formNickList;
    }
    @Override
    public NickVO selectFNickBynickId(int nickId) {
        NickVO nickVO = new NickVO();
        Nick nick = nickMapper.selectById(nickId);
        nickVO.setNickId(nickId);
        nickVO.setNickName(nick.getNickName());
        List<Role> roleList = nickroleMapper.selectAllroleIdBynickId(nick.getNickId());
//        List<String> roleNameList = new ArrayList<>();
//        for(Integer roleId:roleIdlist){
//            String roleName = RoleMapper.selectroleNameByroleId(roleId);
//            roleNameList.add(roleName);
//        }
        nickVO.setRoleList(roleList);
        return nickVO;
    }


    @Override
    public int addNick(Nick nick) {
        if(nick==null){
            log.info("【后台角色管理】角色信息错误");
            throw new ResultException(ExceptionEnum.DATA_ERROR);
        }
        nickMapper.insert(nick);
        return nick.getNickId();
    }


    @Override
    public void updateNick(Nick nick) {
        if(nick==null){
            log.info("【后台角色管理】角色信息错误");
            throw new ResultException(ExceptionEnum.DATA_ERROR);
        }
        nickMapper.updateById(nick);
    }


    @Override
    public void deleteNick(int nickId) {
        if("0".equals(String.valueOf(nickId))||String.valueOf(nickId)==null){
            log.info("【后台角色管理】角色编号数据出错");
            throw  new ResultException(ExceptionEnum.DATA_ERROR);
        }
        Admin admin = adminMapper.selectAdminBynickId(nickId);
        if(admin!=null){
            log.info("【后台角色管理】该角色存在管理员用户");
            throw new ResultException(ExceptionEnum.LATER_ERROR);
        }
        nickMapper.deleteById(nickId);
    }

    @Override
    public List<Role> SelectAllrole() {
        List<Role> roleList = RoleMapper.selectAllRole();
        return roleList;
    }
}
